﻿using aier.dataaccess.dapper;
using aier.dataaccess.model;
using aier.xrm.wcf;
using log4net;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.business.logic
{
    public class CompetitorBll
    {
        public CompetitorDal competitorDal { get; set; }
        public CommonWcf commonWcf { get; set; }
        public ILog log { get; set; }



        /// <summary>
        /// 更新
        /// </summary>
        /// <returns></returns>
        public ResResult UpdateCompetitor(CompetitorInfo competitorInfo,Guid userid)
        {
            ResResult result = new ResResult();
            try
            {
                if (competitorInfo == null)
                {
                    log.Error("UpdateCompetitor 出现错误：传入对象为空");
                    result.ResultMes = "error:传入对象为空";
                    result.ResultState = 0;
                    return result;
                }
                Entity entity = competitorInfo.AsCrmEntity();
                if (entity.Contains("new_iscoopmedicalcouplet"))
                {
                    if (entity.GetAttributeValue<bool>("new_iscoopmedicalcouplet"))
                    {
                        entity["new_mode"] = new OptionSetValue(1);
                    }
                }
                commonWcf.UpdateEntityProxyById(userid, entity);
                result.ResultMes = "success";
                result.ResultState = 1;
                return result;
            }
            catch (Exception e)
            {
                log.Error("UpdateCompetitor 出现错误：" + e.Message);
                result.ResultMes = "error:" + e.Message;
                result.ResultState = 0;
                return result;
            }
        }

        public CompetitorView QueryCompetitor(Guid competitorId)
        {
            CompetitorView competitorView = new CompetitorView();
            competitorView.competitorid = competitorId;
            return this.QueryCompetitor(competitorView)?.FirstOrDefault();
        }

        /// <summary>
        /// 根据不同条件查询医疗机构-竞争对手,只要属性不为空就视为查询条件
        /// 查询条件为：医疗机构ID/省份ID/市ID/区ID/社区ID/名称
        /// 原程序 LocationQueryServiceImpl/queryCompetitor
        /// </summary>
        /// <param name="competitorView">医疗机构对象</param>
        /// <returns></returns>
        public List<CompetitorView> QueryCompetitor(CompetitorView competitorView)
        {
            List<CompetitorView> competitorViews = competitorDal.QueryCompetitor(competitorView);
            return competitorViews;
        }
        /// <summary>
        /// 根据不同条件查询医疗机构-竞争对手,只要属性不为空就视为查询条件
        /// 查询条件为：医疗机构ID/省份ID/市ID/区ID/名称
        /// 原程序 LocationQueryServiceImpl/queryCompetitorCount
        /// </summary>
        /// <param name="competitorView">医疗机构对象</param>
        /// <returns></returns>
        public int QueryCompetitorCount(CompetitorView competitorView)
        {
            int count = competitorDal.QueryCompetitorCount(competitorView);
            return count;
        }
    }
}
